﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SourceCode.SmartObjects.Client;
using SourceCode.Hosting.Client;
using SourceCode.Workflow.Client;

namespace Eform.Data
{
    public class ProcessAction
    {
        public static void InsertHistoryComment(string eformId, string DomainAccount)
        {
            SOServer server = SOServer.Instance();
            server.Open();
            SmartObject comment = server.GetSmartObject("HistoryComment");

            comment.Properties["RequestID"].Value = eformId;
            comment.Properties["DomainAccount"].Value = DomainAccount;
            comment.Properties["Date"].Value = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
            comment.Properties["SignatureCaption"].Value = SignatureCaption.Approver.ToString();

            comment.MethodToExecute = "Create";
            server.ExecuteScalar(comment);
            server.Close();
        }

        public static void UpdateProStatus(string eFormProcID, ProcStatus status)
        {
            SOServer server = SOServer.Instance();
            server.Open();
            SmartObject process = server.GetSmartObject("Process");

            //Set properties
            process.Properties["ID"].Value = eFormProcID;
            process.Properties["Status"].Value = ((int)status).ToString(); //(0: In process, 1: Complete, 2: Rework, 3:Reject)

            process.MethodToExecute = "Save";
            server.ExecuteScalar(process);
            server.Close();
        }

        public static int GetProInstID(string eFormProcCode)
        {
            SOServer server = SOServer.Instance();
            server.Open();
            SmartObject process = server.GetSmartObject("Process");

            process.Properties["Code"].Value = eFormProcCode;
            process.MethodToExecute = "GetList";
            SmartObjectList SoList = server.ExecuteList(process);

            int result = default(int);
            if (SoList.SmartObjectsList.Count > 0)
            {
                result = Convert.ToInt32(SoList.SmartObjectsList[0].Properties["K2ProcessID"].Value);
            }

            server.Close();
            return result;
        }

        public static int GetEformID(string eFormProcCode)
        {
            SOServer server = SOServer.Instance();
            server.Open();
            SmartObject process = server.GetSmartObject("Process");

            process.Properties["Code"].Value = eFormProcCode;
            process.MethodToExecute = "GetList";
            SmartObjectList SoList = server.ExecuteList(process);

            int result = default(int);
            if (SoList.SmartObjectsList.Count > 0)
            {
                result = Convert.ToInt32(SoList.SmartObjectsList[0].Properties["ID"].Value);
            }

            server.Close();
            return result;
        }

        public static string GetEformCode(string EformProcID)
        {
            SOServer server = SOServer.Instance();
            server.Open();
            SmartObject process = server.GetSmartObject("Process");

            process.Properties["ID"].Value = EformProcID;
            process.MethodToExecute = "GetList";
            SmartObjectList SoList = server.ExecuteList(process);

            string result = default(string);
            if (SoList.SmartObjectsList.Count > 0)
            {
                result = Convert.ToString(SoList.SmartObjectsList[0].Properties["Code"].Value);
            }

            server.Close();
            return result;
        }

        public static void UpdateDataField(int procInstId, string dataFieldName, string newValue)
        {
            K2Server server = K2Server.Instance();
            server.Open();

            //Create process instance
            ProcessInstance objBPProcessInst;
            objBPProcessInst = server.OpenProcessInstance(procInstId);

            foreach (DataField dField in objBPProcessInst.DataFields)
            {
                //Check which FataField you want to update
                if (dField.Name == dataFieldName)
                {
                    dField.Value = newValue;
                    // Call Update Method to update the DatField value
                    objBPProcessInst.Update();
                }
            }

            server.Close();
        }
    }
}
